﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Seagull.BarTender.Print;

namespace LabelPrint.Utils
{
    public class ZebraPrinter
    {
        private bool _isPrint;
        private string _printerName;
        private Engine _zebraEngine = null;
        private LabelFormatDocument _btFormat = null;
        private Dictionary<string, string> _printFormat = new Dictionary<string, string>();

        /// <summary>
        /// 打印机名称
        /// </summary>
        public string PrinterName
        {
            get
            {
                return _printerName;
            }
        }

        /// <summary>
        /// 正在打印
        /// </summary>
        public bool IsPrint
        {
            get
            {
                return _isPrint;
            }
        }

        /// <summary>
        /// 打印模板内容
        /// </summary>
        public Dictionary<string, string> PrintFormat
        {
            get
            {
                return _printFormat;
            }
        }

        public ZebraPrinter()
        {
        }

        ~ZebraPrinter()
        {
        }

        /// <summary>
        /// 实例化Zebra打印机
        /// </summary>
        /// <param name="printerName">打印机名称</param>
        public ZebraPrinter(string printerName)
        {
            _printerName = printerName;
            try
            {
                _zebraEngine = new Engine(true);
            }
            catch (PrintEngineException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 开启打印引擎
        /// </summary>
        /// <param name="filePath">模板路径</param>
        private void Start(string filePath)
        {
            try
            {
                _zebraEngine.Start();
                _btFormat = _zebraEngine.Documents.Open(filePath);
            }
            catch (PrintEngineException ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 停止打印引擎
        /// </summary>
        private void Stop()
        {
            try
            {
                _zebraEngine.Stop();
            }
            catch (PrintEngineException ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 传输打印数据
        /// </summary>
        /// <param name="printFormat"></param>
        /// <returns></returns>
        private int TransferPrintData(Dictionary<string, string> printFormat)
        {
            try
            {
                if (printFormat.Count > 0)//有数据才传
                {
                    foreach (var item in printFormat)
                    {
                        _btFormat.SubStrings[item.Key].Value = item.Value;
                    }
                }
                return printFormat.Count;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <returns>返回打印结果</returns>
        private Result Print()
        {
            try
            {
                _btFormat.PrintSetup.PrinterName = _printerName;
                Result resPrinter = _btFormat.Print();
                return resPrinter;
            }
            catch
            {
                return Result.Failure;
            }
        }

        /// <summary>
        /// 打印动作
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="printFormat">打印数据</param>
        /// <returns>返回打印结果</returns>
        public string PrintAction(string filePath, Dictionary<string, string> printFormat)
        {
            Start(filePath);
            TransferPrintData(printFormat);
            Result result = Print();
            Stop();
            switch (result)
            {
                case Result.Success:
                    return "Success";

                case Result.Failure:
                    return "Failed";

                case Result.Timeout:
                    return "Timeout";
            }
            return "NULL";
        }
    }
}